const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
function IdCard(UUserCard, num) {
      if (num == 1) {
          //获取出生日期
          var birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12);
  //         var birth = UUserCard.substring(6, 10) + "-" + UUserCard.substring(10, 12) + "-" + UUserCard.substring(12, 14);
          return birth;
      }
      if (num == 2) {
          //获取性别
          if (parseInt(UUserCard.substr(16, 1)) % 2 == 1) {
              //男
              return "男";
          } else {
              //女
              return "女";
          }
      }
      if (num == 3) {
          //获取年龄
          var myDate = new Date();
          var month = myDate.getMonth() + 1;
          var day = myDate.getDate();
          var age = myDate.getFullYear() - UUserCard.substring(6, 10) - 1;
          if (UUserCard.substring(10, 12) < month || UUserCard.substring(10, 12) == month && UUserCard.substring(12, 14) <= day) {
              age++;
          }
          return age;
      }
}
const aCity = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外" };

let DES3 = require("DES3.js");
let BASE64 = require("Base64.js");
function DES3_Encrypt(data) {
  let key = '123456';
  var des3en = BASE64.encoder(DES3.encrypt(key, data));
  return des3en;
}
function DES3_Decrypt(data) {
  let key = '123456';
  var des3de = DES3.decrypt(key, BASE64.decoder(data));
  return des3de;
}
 //验证身份证号并获取籍贯
 function getProvinceNameByIdNo(idcard) {
  var area = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古",
      21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏",
      33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北",
      43: "湖南", 44: "广东", 45: "广西",
      46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西",
      62: "甘肃", 63: "青海", 64: "宁夏",
      65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外"
  }

  var provinceName = "";
  var provinceNo = idcard.substr(0, 2);
  if (area[parseInt(provinceNo)] != null) {
      provinceName = area[parseInt(provinceNo)];
  }
  return provinceName;
}
//验证身份证格式
const IdentityCodeValid = sId => {
  var iSum = 0;
  var info = "";
  if (!/^\d{17}(\d|X|x)$/i.test(sId)) return false;
  sId = sId.replace(/x$/i, "a");
  if (aCity[parseInt(sId.substr(0, 2))] == null) return false;
  var sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
  var d = new Date(sBirthday.replace(/-/g, "/"));
  if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return false;
  for (var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
  if (iSum % 11 != 1) return false;
  //aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女");//此次还可以判断出输入的身份证号的人性别
  return true;
}


//手机号格式
const regPhone = phone =>{
  var reg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
  return reg.test(phone);
}

const nowTime =()=>{
  //获取当前时间  
  var date = new Date();
  //年  
  var Y = date.getFullYear();
  //月  
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //日  
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  //时  
  var h = date.getHours();
  //分  
  var m = date.getMinutes();
  //秒  
  var s = date.getSeconds();
  var establishTime =  M + "-" + D + " " + h + ":" + m + ":" + s
  // var establishTime = Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s
  return establishTime;
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const imgName = () => {
  //获取当前时间  
  var date = new Date();
  //年  
  var Y = date.getFullYear();
  //月  
  var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
  //日  
  var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
  //时  
  var h = date.getHours();
  //分  
  var m = date.getMinutes();
  //秒  
  var s = date.getSeconds();
  var establishTime = Y + M + D + h + m + s
  return establishTime;
}
//上传语音
function up_audio(url, audioSrc, name, data, _succ, _fail) {
  const uploadTask = wx.uploadFile({
    url: url, //仅为示例，非真实的接口地址
    filePath: audioSrc,
    name: name,
    formData: data,
    header: {
      "content-type": "multipart/form-data"
    },
    success: function (res) {
      _succ(res)
    }, fail: function (err) {
      _fail(err)
    }
  })
  uploadTask.onProgressUpdate((res) => {
    console.log('audio上传进度', res.progress)
    console.log('audio已经上传的数据长度', res.totalBytesSent)
    console.log('audio预期需要上传的数据总长度', res.totalBytesExpectedToSend)
  })
}
 
/*
 * @param x {Object} 对象1
 * @param y {Object} 对象2
 * @return  {Boolean} true 为相等，false 为不等
 */
var deepEqual = function (x, y) {
  // 指向同一内存时
  if (x === y) {
    return true;
  }
  else if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) {
    if (Object.keys(x).length != Object.keys(y).length)
      return false;
 
    for (var prop in x) {
      if (y.hasOwnProperty(prop))
      {  
        if (! deepEqual(x[prop], y[prop]))
          return false;
      }
      else
        return false;
    }
 
    return true;
  }
  else 
    return false;
}
function ages(str)
{
var r = str.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if(r==null)return false;
var d= new Date(r[1], r[3]-1, r[4]);
if (d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4])
{
var Y = new Date().getFullYear();
return(Y-r[1]);
}
}


module.exports = {
  formatTime: formatTime,IdentityCodeValid ,regPhone, deepEqual,
  nowTime: nowTime, ages,
  DES3_Encrypt, DES3_Decrypt,
  imgName: imgName, 
  up_audio,
  IdCard,getProvinceNameByIdNo
}
